<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by an Apache 2.0 License.
See the COPYING file for details.
-->
<head>
<title>Closure Unit Tests - goog.dom.TextRange</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.dom');
  goog.require('goog.dom.Range');
  goog.require('goog.dom.ControlRange');
  goog.require('goog.dom.TextRange');
  goog.require('goog.testing.dom');
  goog.require('goog.testing.jsunit');
  goog.require('goog.userAgent');
</script>
</head>
<body>
  <div id="test1"></div>
  <div id="test2">
    <img id="logo" src="http://www.google.com/intl/en_ALL/images/logo.gif">
  </div>
  <!-- Omit whitespace here to ensure no extra text nodes are included. -->
  <table id="table"><tbody id="tbody"><tr id="tr1"><td id="td11">a</td
      ><td id="td12">b</td></tr><tr id="tr2"><td id="td21">c</td><td id="td22"
      >d</td></tr></tbody></table>
  <table id="table2">
    <tr>
      <td>moof</td>
    </tr>
    <tr>
      <td id="table2td">
        <div id="table2div">foo<img id="logo2"
               src="http://www.google.com/intl/en_ALL/images/logo.gif">bar</div>
      </td>
    </tr>
  </table>


<script>
  var logo = goog.dom.getElement('logo');
  var logo2 = goog.dom.getElement('logo2');
  var table = goog.dom.getElement('table');
  var table2 = goog.dom.getElement('table2');
  var table2div = goog.dom.getElement('table2div');

  function testCreateFromNodeContents() {
    assertNotNull('Text range object can be created for element node',
        goog.dom.TextRange.createFromNodeContents(logo));
    assertNotNull('Text range object can be created for text node',
        goog.dom.TextRange.createFromNodeContents(logo2.previousSibling));
  }

  function testMoveToNodes() {
    var range = goog.dom.TextRange.createFromNodeContents(table2);
    range.moveToNodes(table2div, 0, table2div, 1, false);
    assertEquals('Range should start in table2div',
                 table2div,
                 range.getStartNode());
    assertEquals('Range should end in table2div',
                 table2div,
                 range.getEndNode());
    assertEquals('Range start offset should be 0',
                 0,
                 range.getStartOffset());
    assertEquals('Range end offset should be 0',
                 1,
                 range.getEndOffset());
    assertFalse('Range should not be reversed',
                range.isReversed());
    range.moveToNodes(table2div, 0, table2div, 1, true);
    assertTrue('Range should be reversed',
               range.isReversed());
    assertEquals('Range text should be "foo"',
                 'foo',
                 range.getText());
  }

  function testContainsTextRange() {
    var range = goog.dom.TextRange.createFromNodeContents(table2);
    var range2 = goog.dom.TextRange.createFromNodeContents(table2div);
    assertTrue('TextRange contains other TextRange',
            range.containsRange(range2));
    assertFalse('TextRange does not contain other TextRange',
            range2.containsRange(range));

    range = goog.dom.Range.createFromNodes(
            table2div.firstChild, 1, table2div.lastChild, 1);
    range2 = goog.dom.TextRange.createFromNodes(
            table2div.firstChild, 0, table2div.lastChild, 0);
    if (!goog.userAgent.WEBKIT) {
      // TODO(user): Figure out why this fails, fix it, and get the WebKit
      // folks to fix it :-)
      assertTrue('TextRange partially contains other TextRange',
              range2.containsRange(range, true));
    }
    assertFalse('TextRange does not fully contain other TextRange',
            range2.containsRange(range, false));
    
  }

  function testContainsControlRange() {
    if (goog.userAgent.IE) {
      var range = goog.dom.ControlRange.createFromElements(table2);       
      var range2 = goog.dom.TextRange.createFromNodeContents(table2div);
      assertFalse('TextRange does not contain ControlRange',
              range2.containsRange(range));
      range = goog.dom.ControlRange.createFromElements(logo2);
      assertTrue('TextRange contains ControlRange',
              range2.containsRange(range));
      range = goog.dom.TextRange.createFromNodeContents(table2);
      range2 = goog.dom.ControlRange.createFromElements(logo, logo2);
      assertTrue('TextRange partially contains ControlRange',
              range2.containsRange(range, true));
      assertFalse('TextRange does not fully contain ControlRange',
              range2.containsRange(range, false));
    }
  }

</script>
</body>
</html>
